package com.zd.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;


/**
 * URL与session拦截器
 */



//@Configuration
//@ComponentScan( useDefaultFilters = true)*/
public class SecurityConfig extends WebMvcConfigurationSupport {

    //读取配置文件获取上传文件夹路径
    @Value("${upload.uploadPath}")
    private String uploadPath;

    public final static String SESSION_KEY = "userInfo";
    //private ApplicationContext applicationContext;

    @Bean
    public SecurityInterceptor getSecurityInterceptor() {
        return new SecurityInterceptor();
    }




    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration addInterceptor=registry.addInterceptor(new SecurityInterceptor());
        //排除的路径
        //登录不拦截

        addInterceptor.excludePathPatterns("/");


        addInterceptor.excludePathPatterns("/login");



        //addInterceptor.excludePathPatterns("/static**");*/

        //addInterceptor.excludePathPatterns("/workInfo/**");
       //addInterceptor.excludePathPatterns("/userInfo/**");
        //addInterceptor.excludePathPatterns("/findByAdminAndPassword");
        //拦截所有路径
        addInterceptor.addPathPatterns("/**");
        super.addInterceptors(registry);
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //registry.addResourceHandler("/").addResourceLocations("/**");
        registry.addResourceHandler("/staticFile/**").addResourceLocations(ResourceUtils.FILE_URL_PREFIX + uploadPath + File.separator);
        registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"resources/static"+File.separator);
        registry.addResourceHandler("/plugins/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"resources/static/plugins"+File.separator);


    }

    /**
     * 配置servlet处理
     */
    //@Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }


    private class SecurityInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
            //request=((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            HttpSession session = request.getSession();

            //判断是否已有该用户登录的session
            if (session.getAttribute(SESSION_KEY) != null) {
                return true;
            }
            //跳转到登录页
            response.sendRedirect(request.getContextPath()+"/");

            return false;
        }
    }

}
